<template>
	<n-page>
		<view slot="navbar" class="fixed" style="top: 0;left:0;width: 750rpx; height: 200rpx;z-index: 100;">
			<view class="padding-x-md " style="background-image: linear-gradient(to right, #f43f3b,#ec008c);">
				<n-status-bar background="linear-gradient(to right, #f43f3b,#ec008c)"></n-status-bar>
				<view class="flex-column">
					<view class="flex-row justify-between items-center padding-y-xs">
						<view @click="$util.redirectTo('/otherpages/index/city/city')" class="flex-row padding-x-md radius-xl" style="background-color: rgba(255, 255, 255, 0.3);height: 52rpx;">
							<n-icon color="#FFFFFF" name="locationfill" size="26"></n-icon>
							<text class="color-gray-1 padding-left-xs text-sm">{{ city }}</text>
						</view>
						<view class="flex-row">
							<n-icon color="#FFFFFF" name="scan" size="42" @click="sean()"></n-icon>
							<n-icon color="#FFFFFF" name="servicefill" size="42" class="margin-left-sm" @click="$util.redirectTo('/otherpages/chat/room/room?siteId=0')"></n-icon>
							<n-icon color="#FFFFFF" name="notificationfill" class="margin-left-sm" size="42" @click="$util.redirectTo('/otherpages/station/list/list')"></n-icon>
						</view>
					</view>
					
					<view class="input-view margin-bottom-xs" @click="$util.redirectTo('/otherpages/goods/search/search')">
						<n-icon color="#8c8c8c" name="search" size="32" style="line-height: 60rpx;"></n-icon>
						<text class="color-gray-7 text-sm padding-left-xs" style="line-height: 60rpx;">商品搜索</text>
					</view>
				</view>
			</view>
			<view v-if="topIndexValue" style="position: relative;background-color: #fff4ed;">

				<scroll-view ref="tabbar1" id="tab-bar" style="width: 690rpx;" class="tab-bar" :scroll="false" :scroll-x="true" :show-scrollbar="false"
				 :scroll-into-view="'a' + pageIndex">
					<view class="flex-column">
						<view class="flex-row">
							<view class="uni-tab-item" v-for="(tab,index) in cateList" :key="tab.id" :id="'a' + index" @click="changePageIndex(index)">
								<text class="uni-tab-item-title" :class="pageIndex==index ? 'color-red-6' : ''">{{tab.category_name}}</text>
								<view style="height: 6rpx;" :class="pageIndex==index ? 'bg-red-6' : ''"></view>
							</view>
						</view>
					</view>
				</scroll-view>
				<view @click="$util.redirectTo('/pages/goods/category/category')" style="width: 60rpx;right:0;top:0;height: 70rpx;" class="absolute flex-row justify-center items-center">
					<n-icon name="cascades" color="$gray-7" size="38"></n-icon>
				</view>
			</view>
		</view>
		<!-- 头部区域 -->
		<view slot="header">
			<uni-status-bar style="margin-top: 240rpx;" />
		</view>
		<!-- 主体区域 -->
		<view slot="body">
			<block v-if="pageIndex == 0">
				<!-- 页面滚动内容区域 -->
				<view v-if="topIndexValue" class="bg-gray-1">
					<diy-group :diyData="diyData" :height="scrollTopHeight" :scrollHeight="scrollHeight" :city="city"></diy-group>
				</view>
			</block>
			<block v-else>
				<view class="">
					<!-- 二级分类 -->
					<view class="padding-md bg-gray-1 margin-top-xs" v-if="twoCategorylist!='undefined' && twoCategorylist && twoCategorylist.length > 0">
						<swiper class=" " :duration="500" @change="swiperTocategoryChange">
							<swiper-item class="flex-row flex-wrap" v-for="page in maxPage" :key="page">
								<view
									class="flex-row items-center justify-center" style="width: 140rpx;"
									v-for="(item, index) in twoCategorylist"
									:key="index"
									v-if="index >= page * 10 && index < (page + 1) * 10"
									@click="toCateGoodsList(item.category_id_2, 2)">
									<view class="flex-column items-center">
										<n-image width="80" height="80" radius="40" :src="$util.img(item.image)" :placeholder="$util.getDefaultImage().default_goods_img" mode="aspectFill"></n-image>
										<view>{{ item.category_name }}</view>
									</view>
								</view>
							</swiper-item>
						</swiper>
						<view class="flex-row items-center justify-center">
							<n-icon class="margin-left-xs" name="roundrecordfill" :color="twoCategorylistId == page ? '$gray-7' : '$gray-5'" size="10" v-for="page in maxPage" v-if="maxPage.length > 1" :key="page" ></n-icon>
						</view>
					</view>
						
					<!-- 分类广告 -->
					<image class="category_adv ns-margin" v-if="cateList[pageIndex].image_adv" :src="$util.img(cateList[pageIndex].image_adv)" mode="widthFix"></image>
						
					<view class="category-goods"><ns-goods-item-row :value="goodsList[pageIndex].list" @toDetail="toDetail"></ns-goods-item-row></view>
				</view>
			</block>
			
			
			
				
				
			<!-- 弹窗广告 -->
			<template v-if="popWindowBox && popWindowBox.controller == 'PopWindow' && isPopWindowBox">
				<view @touchmove.prevent.stop>
					<uni-popup ref="uniPopupWindow" type="bottom" class="flex-column" :maskClick="false">
						<view class="flex-1">
							<image style="width: 100%;" :src="popWindowBox.image_url" mode="widthFix" @click="redirectTo(popWindowBox.link)"></image>
						</view>
						<text class="iconfont iconroundclose absolute text-xl" style="right:20rpx;top:10rpx" @click="closeNum"></text>
					</uni-popup>
				</view>
			</template>
			<!-- 站点关闭展示 -->
			<view @touchmove.prevent.stop class="PopWindow">
				<uni-popup ref="uniPopupClose" type="center" class="wap-floating" :maskClick="false">
					<view class="popup-box">
						<view class="close_title ns-margin-top">站点关闭</view>
						<view class="close_content">
							<scroll-view scroll-y="true" class="close_content_box">{{ webSiteInfo.close_reason }}</scroll-view>
						</view>
					</view>
				</uni-popup>
			</view>
			<!-- 版本更新 -->
			<uni-popup ref="popup_update">
				<view class="pay-methods">
					<view class="tip">版本更新</view>
					<view class="sub_tip">新版本已经准备好，是否重启应用？</view>
					<view class="sub_tip2">本次更新内容:</view>
					<view class="msg-box">
						<rich-text :nodes="upApp"></rich-text>
					</view>
					<view class="btn-group">
						<button type="default" class="back-btn" @click="back_update()">取消</button>
						<button type="default" class="sue-btn" @click="go_update()">立即更新</button>
					</view>
				</view>
			</uni-popup>
			<uni-popup ref="sean_popup" type="center" class="wap-floating">
				<view class="title">
					确定{{poster_msg.user_name}}绑定为你的上级吗?
				</view>
				<view class="img-box">
					<image :src="$util.img(poster_msg.user_img)" mode=""></image>
				</view>
				<view class="message-box">
					<text>推荐码:</text>
					<text class="tuijian-code">{{poster_msg.invite_code}}</text>
				</view>
				<view class="btn-group">
					<button type="default" class="quxiao" @click="close_popup(1)">再想想</button>
					<button type="default" class="sue" @click="sue_popup()">确定</button>
				</view>
				<!-- <text class="iconfont iconroundclose" @click="closeNum"></text> -->
			</uni-popup>
			<uni-popup ref="sean_popup2" type="center" class="wap-floating">
			
				<view class="title">
					已经绑定{{poster_msg.user_name}}为你的上级
				</view>
				<view class="img-box">
					<image :src="$util.img(poster_msg.user_img)" mode=""></image>
				</view>
				<view class="message-box">
					<text>推荐码:</text>
					<text class="tuijian-code">{{poster_msg.invite_code}}</text>
				</view>
				<button type="default" class="sueed" @click="close_popup(2)">确定</button>
				<!-- <text class="iconfont iconroundclose" @click="closeNum"></text> -->
			</uni-popup>
			
			<ns-login ref="login"></ns-login>
			<loading-cover ref="loadingCover"></loading-cover>
		</view>
		<!-- 底部区域 -->
		<view slot="footer">
			<diy-bottom-nav v-if="openBottomNav" @isBulge="isBulge" @callback="callback"></diy-bottom-nav>
		</view>
	</n-page>
</template>

<script>
	import nsGoodsItemRow from '@/components/ns-goods-item/ns-goods-item-row.vue';
	
	import diyHorzBlank from '@/components/diy-horz-blank/diy-horz-blank.vue';
	import diyHorzLine from '@/components/diy-horz-line/diy-horz-line.vue';
	import diyCoupon from '@/components/diy-coupon/diy-coupon.vue';
	import diyGoodsList from '@/components/diy-goods-list/diy-goods-list.vue';
	import diyImgAds from '@/components/diy-img-ads/diy-img-ads.vue';
	import diyGraphicNav from '@/components/diy-graphic-nav/diy-graphic-nav.vue';
	import diyNotice from '@/components/diy-notice/diy-notice.vue';
	import diyRichText from '@/components/diy-rich-text/diy-rich-text.vue';
	import diyRubikCube from '@/components/diy-rubik-cube/diy-rubik-cube';
	import diySearch from '@/components/diy-search/diy-search.vue';
	import diyTextNav from '@/components/diy-text-nav/diy-text-nav.vue';
	import diyTitle from '@/components/diy-title/diy-title.vue';
	import diyVideo from '@/components/diy-video/diy-video.vue';
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import diySeckill from '@/components/diy-seckill/diy-seckill.vue';
	import diyBargain from '@/components/diy-bargain/diy-bargain.vue';
	import diyBottomNav from '@/components/diy-bottom-nav/diy-bottom-nav.vue';
	import diyPintuan from '@/components/diy-pintuan/diy-pintuan.vue';
	import diyGroupBuy from '@/components/diy-groupbuy/diy-groupbuy.vue';
	import diyGoodsLevelCategory from '@/components/diy-goods-level-category/diy-goods-level-category.vue';
	import diyLive from '@/components/diy-live/diy-live.vue';
	import WxMap from 'common/js/map-wx-jssdk.js';
	import {
		Weixin
	} from 'common/js/wx-jssdk.js';
	import globalConfig from '@/common/js/golbalConfig.js';
	import diyWholeSale from '@/components/diy-wholesale/diy-wholesale.vue';
	import config from '@/common/js/config.js';
	export default {
		components: {
			nsGoodsItemRow,
			
			diyHorzBlank,
			diyHorzLine,
			diyCoupon,
			diyGoodsList,
			diyImgAds,
			diyGraphicNav,
			diyNotice,
			diyRichText,
			diyRubikCube,
			diySearch,
			diyTextNav,
			diyTitle,
			diyVideo,
			diyBargain,
			uniPopup,
			diySeckill,
			diyBottomNav,
			diyPintuan,
			diyGroupBuy,
			diyGoodsLevelCategory,
			diyLive,
			diyWholeSale
		},
		data() {
			return {
				token: '',
				searchText: '',
				cityAddon: 0,
				poster_msg: {
					invite_code: '',
					user_img: '',
					user_name: ''
				} ,//扫码时二维码里面的信息
				
				pageIndex: 0, //当前选中分类id
				cateList: [
					{
						//header分类
						category_name: '首页'
					}
				],
				twoCategorylist: [], //二级分类
				twoCategorylistId: 0, //二级分类所在的swiper
				goodsList: {},
				scrollTop: 0,
				scrollTopCopy: 0,
				defaultImage: this.$util.getDefaultImage().default_goods_img,
				
				diyData: [],
				webSiteInfo: null,
				memberId: 0,
				id: 0,
				city: '',
				siteId: 0,
				windowHeight: 0, //分类组件的高度
				Bulge: false, //判断底部导航是否凸起
				TopCategory: {},

				popWindowBox: null, //弹窗广告
				pageHeight: '0', //屏幕可用高度
				headerHeight: '0', //头部高度
				topIndexValue: null, //首页顶部分类

				bgTopColor: '',
				textColor: '',
				upApp: '',
				upCode: '',
			
				// 弹框是否弹出状态
				isPopWindowBox: false,
				// 当前时间
				currentTime: 0,
				//真实路径
				trueUrl:'',
			};
		},
		computed: {
			maxPage() {
				let num = 0;
				let array=[];
				if (this.twoCategorylist && this.twoCategorylist.length) {
					num = Math.ceil(this.twoCategorylist.length / 10);
				}
				for(let i=0;i<num;i++){
					array.push(i)
				}
				return array;
			},
			// #ifdef APP-PLUS

			// #endif
			bottomHeight() {
				// if(this.Bulge){
				// 	return 90 + 'px'
				// }else{
				return 55 + 'px'
				// }
			},
			bgColor() {
				let str = '';
				if (this.diyData && this.diyData.global) {
					str = this.diyData.global.bgColor;
				}
				return str;
			},
			bgUrl() {
				let str = '';
				if (this.diyData && this.diyData.global) {
					str = this.diyData.global.bgUrl;
				}
				return str;
			},
			pageTitle() {
				let str = '';
				if (this.diyData && this.diyData.global) {
					str = this.diyData.global.title;
				}
				return str;
			},
			openBottomNav() {
				let Bloon = false;
				if (this.diyData && this.diyData.global) {
					Bloon = this.diyData.global.openBottomNav;
				}
				return Bloon;
			},
			navBar() {
				let str = '#ffffff';
				if (this.bgUrl) {
					str = 'url(' + this.$util.img(this.bgUrl) + ') no-repeat 0 0/100%';
				} else if (this.bgColor) {
					str = this.bgColor;
				} else {
					str = 'rgba(0,0,0,0)';
				}
				return str;
			},

			scrollHeight() {
				if (this.pageHeight != undefined && this.headerHeight != undefined && this.bottomHeight != undefined) {
					// #ifdef APP-PLUS || MP-WEIXIN
					return 'calc(' + this.pageHeight + ' - ' + this.headerHeight +' - env(safe-area-inset-bottom)' + ' - ' + this.bottomHeight +
						')';

					// #endif
					// #ifdef H5
					return 'calc(' + this.pageHeight + ' - env(safe-area-inset-bottom)' + ' - ' + this.bottomHeight + ')';
					// #endif

				} else {
					return '100vh';
				}
			},
			scrollTopHeight() {
				if (this.pageHeight != undefined && this.headerHeight != undefined && this.bottomHeight != undefined) {
					return 'calc(' + this.pageHeight + ' - ' + this.headerHeight + ' - env(safe-area-inset-bottom)' + ' - ' + this.bottomHeight +
						' - 80rpx)';
				} else {
					return '100vh';
				}
			}
		},
		mixins: [globalConfig],
		//下拉刷新
		onPullDownRefresh() {
			this.getDiyInfo();
		},
		created() {
			this.getCategoryList();
		},
		onLoad(data) {
			this.siteId = parseInt(data.site_id) || 0;
			// 如果传入站点id，则清空地址
			if (this.siteId) uni.removeStorageSync('city');

			if (data.source_member) uni.setStorageSync('source_member', data.source_member);
			// 小程序扫码进入
			if (data.scene) {
				var sceneParams = decodeURIComponent(data.scene);
				sceneParams = sceneParams.split('&');
				if (sceneParams.length) {
					sceneParams.forEach(item => {
						if (item.indexOf('source_member') != -1) uni.setStorageSync('source_member', item.split('-')[1]);
					});
				}
			}
			// 版本更新
			this.checkVersionUpdate()
			//操作总体控制
			if(data.action){
				this.actionController(data);
			}
		},
		onShow() {
			this.$langConfig.refresh();
			this.getHeight();
			if (uni.getStorageSync('token')) {
				this.getMemberId();
			}

			this.getWebSitefo();
			this.getDefaultImg();

			if (uni.getStorageSync('city')) {
				this.city = uni.getStorageSync('city').title;
				this.getDiyInfo();
			} else {
				uni.setStorageSync('city', {
					id: 0,
					title: '全国'
				});
				this.city = '全国';
				this.getDiyInfo();
			}

		},
		onHide() {
			try {
				this.pageIndex = 0
			} catch (e) {}

		},
		methods: {
			//请求分类列表
			getCategoryList() {
				let url = '/api/goodscategory/tree';
				let data = {
					level: 3,
					template: 2
				};
				if (this.siteId) {
					data.site_id = this.siteId;
					url = '/api/shopgoodscategory/tree';
				}
				this.$api.sendRequest({
					url: url,
					data: data,
					success: res => {
						if (res.code >= 0) {
							let arr = [];
							let obj = {};
							obj.category_name = '首页';
							arr.push(obj);
							this.cateList = arr.concat(res.data);
							Object.keys(this.cateList).forEach((key, index) => {
								this.goodsList[key] = {
									page: 1,
									list: [],
									isAll: false
								};
							});
							this.twoCategorylist = this.cateList[this.pageIndex].child_list;
						}
					}
				});
			},
			
			//修改当前页面id
			changePageIndex(index) {				
				this.pageIndex = index;
				if (index == 0) return;
				this.getCategoryList()
				this.twoCategorylist = this.cateList[this.pageIndex].child_list;
				if(this.cateList[this.pageIndex].child_list){
					this.twoCategorylist = this.cateList[this.pageIndex].child_list;
					this.twoCategorylist.forEach(v => {
						if (v.image) {
							v.image = this.$util.img(v.image);
						} else {
							v.image = this.$util.getDefaultImage().default_goods_img;
						}
					});
				}else{
					this.twoCategorylist=false
				}
				let id = index;
				if (this.goodsList[id] && this.goodsList[id].length) return;
				this.scrollTop = this.scrollTopCopy;
				this.$nextTick(function() {
					this.scrollTop = 0;
				});
				this.getGoodsList();
			},
			//swiper  监听变化
			swiperChange(e) {
				this.changePageIndex(e.detail.current);
			},
			
			//监听二级分类  页面切换
			swiperTocategoryChange(e) {
				this.twoCategorylistId = e.detail.current;
			},
			
			toDetail(item) {
				this.$util.redirectTo('/pages/goods/detail/detail', {
					sku_id: item.sku_id
				});
			},
			
			scroll(e) {
				this.scrollTopCopy = e.detail.scrollTop;
			},
			
			getGoodsList() {
				let id = this.pageIndex;
				var data = {
					page: this.goodsList[id].page,
					page_size: 10,
					site_id: this.siteId
				};
				data.category_id = this.cateList[this.pageIndex].category_id_1;
				data.category_level = 1;
				this.$api.sendRequest({
					url: '/api/goodssku/page',
					data: data,
					success: res => {
						if (res.code == 0 && res.data) {
							this.goodsList[id].list = this.goodsList[id].list.concat(res.data.list);
							if (this.goodsList[id].list.length == res.data.count) {
								this.goodsList[id].isAll = true;
							}
							this.goodsList[id].page += 1;
						}
						this.$forceUpdate();
					}
				});
			},
			
			//滚动到底部
			scrolltolower() {
				let id = this.pageIndex;
				if (this.goodsList[id].isAll) return;
				this.getGoodsList();
			},
			toCateGoodsList(e, f) {
				this.$util.redirectTo('/pages/goods/list/list', { category_id: e, category_level: f });
			},
			
			
			
			
			
			sean() {
				this.token = uni.getStorageSync('token');
				// 验证登录
				if (!this.token) {
					this.$util.redirectTo('/pages/login/login/login', {
						back: '/pages/index/index/index'
					});
					return;
				}
				uni.scanCode({
					success: (res) => {
						console.log(res);
						if(-1 != res.result.indexOf('/pages/index/index/index')){
							//首页有公共处理
							let arr = res.result.split('/h5/');
							this.$util.redirectTo('/' + arr[1], {}, 'reLaunch');
						}else if(-1 != res.result.indexOf('/otherpages/shop/index/index')){
							//进入商家
							let arr = res.result.split('/h5/');
							this.$util.redirectTo('/' + arr[1], {}, 'reLaunch');
						}else if(-1 != res.result.indexOf('/login/register/phone_reg')){
							//注册/绑定邀请人
							this.seanResBindInviteRelation(res.result);
						}else if(-1 != res.result.indexOf('/otherpages/verification/detail/detail')){
							let arr = res.result.split('/h5/');
							this.$util.redirectTo('/' + arr[1], {}, 'reLaunch');
						}
					}
				});
			},
			//扫码注册
			seanResBindInviteRelation(res){
				console.log(res);
				let string1 = res.split('?');
				let string2 = string1[1].split('=');
			
				this.poster_msg.invite_code = string2[1]
				this.GetpostMsg(this.poster_msg.invite_code)
			},
			
			close_popup(e) {
				if (e == 1) {
					this.$refs.sean_popup.close()
				} else {
					this.$refs.sean_popup2.close()
				}
			},
			sue_popup(e) {
				this.close_popup(1)
				// 提交绑定
				this.$api.sendRequest({
					url: '/api/member/updateInviteRelation',
					data: {
						token: uni.getStorageSync('token'),
						source_member: this.poster_msg.user_id
					},
					success: res => {
						console.log(res)
						if (res.code == 0) {
							uni.showToast({
								title: '绑定成功~'
							})
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					}
				});
			},
			GetpostMsg(e) {
				this.$api.sendRequest({
					url: '/api/member/scanBindVerify',
					data: {
						token: uni.getStorageSync('token'),
						invite_code: e
					},
					success: res => {
						console.log(res)
						if (res.code == 0) {
							this.poster_msg.user_img = res.data.source_member_info.headimg
							this.poster_msg.user_name = res.data.source_member_info.show_name
							this.poster_msg.user_id = res.data.source_member_info.member_id
							if (res.data.is_bind == 0) {
								this.$refs.sean_popup.open()
							} else {
								this.$refs.sean_popup2.open()
							}
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					}
				});
			},
			redirectToLink(url) {
				if (!uni.getStorageSync('token')) {
					this.$refs.login.open(url);
				} else {
					this.$util.redirectTo(url);
				}
			},
			//+++++++++++++++++
			
			
			
			// 版本更新
			async checkVersionUpdate(formData) {
				let version = config.version_code
				switch (uni.getSystemInfoSync().platform) {
					case 'android':
						this.$api.sendRequest({
							url: '/appversion/api/version/latestInfo',
							data: {
								system_type: "Android"
							},
							success: res => {
								if (res.code >= 0 && res.data && res.data.version_code > version) {
									// 升级提示
									this.upApp = res.data.update_log
									this.upCode = res.data.version_code
									this.$refs.popup_update.open()
								}
							}
						});
						break;
					case 'ios':
						this.$api.sendRequest({
							url: '/appversion/api/version/latestInfo',
							data: {
								system_type: "IOS"
							},
							success: res => {
								// console.log(res)
								if (res.code >= 0 && res.data && res.data.version_code > version) {
									// 升级提示
									this.upApp = res.data.update_log
									this.$refs.popup_update.open()
								}
							}
						});
						break;
					default:
				}
			},
			back_update() {
				this.$refs.popup_update.close()
			},
			go_update() {
				this.$refs.popup_update.close()
				// 跳外网
				if (uni.getSystemInfoSync().platform == "android") {
					this.$api.sendRequest({
						url: '/appversion/api/version/info',
						data: {
							system_type: "Android",
							version_code: this.upCode
						},
						success: res => {
							plus.runtime.openURL(res.data.download_url);
							// console.log(res.data.download_url)
						}
					});
				} else {
					plus.runtime.openURL(config.appleUrl)
					// console.log(config.appleUrl)
				}
			},

			callback() {
				this.pageIndex = 0
			},
			//计算高度
			getHeight() {
				var self = this;
				//获取页面可用区域的高度
				uni.getSystemInfo({
					success: res => {
						self.pageHeight = res.windowHeight + 'px';
						self.windowHeight = res.windowHeight - 57;
					}
				});
				// #ifdef APP-PLUS || MP
				this.$nextTick(() => {
					const query = uni.createSelectorQuery().in(this);
					query
						.select('.page-header')
						.boundingClientRect(data => {
							this.headerHeight = data.height + 'px';
						})
						.exec();
				});
				// #endif

			},

			isBulge(e) {
				this.Bulge = e;
			},
			getDiyInfo() {
				let _this = this;
				
				this.$api.sendRequest({
					url: '/api/diyview/info',
					data: {
						name: 'DIYVIEW_INDEX',
						site_id: this.siteId
					},
					success: res => {
						// 当前时间戳
						this.currentTime = res.timestamp;
						
						if (res.code != 0 || !res.data) {
							if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
							return;
						}
						this.diyData = res.data;
						// if (this.diyData.site_area_id) {
						// 	this.city = this.diyData.site_area_name;
						// 	uni.setStorageSync('city', { id: this.diyData.site_area_id, title: this.diyData.site_area_name });
						// }

						if (this.diyData.value) {
							this.diyData = JSON.parse(this.diyData.value);
							this.bgTopColor = this.diyData.global.bgTopColor;
							this.textColor = this.diyData.global.textColor;
							for (var i = 0; i < this.diyData.value.length; i++) {
								if (this.diyData.value[i].controller == 'TopCategory') {
									this.topIndexValue = this.diyData.value[i];
								}
								
								
								
								
								// 原代码
								/* if (this.diyData.value[i].controller == 'PopWindow') {
									this.popWindowBox = this.diyData.value[i];
									this.popWindowBox.image_url = this.$util.img(this.popWindowBox.image_url);
									setTimeout(() => {
										if (uni.getStorageSync('index_wap_floating_layer') != null && uni.getStorageSync(
												'index_wap_floating_layer') != '') {
											var wap_floating_cookie = JSON.parse(uni.getStorageSync('index_wap_floating_layer'));
											if (wap_floating_cookie.closeNum < Number(this.diyData.value[i].showCount)) {
												this.$refs.uniPopupWindow.open();
											}
										} else {
											this.$refs.uniPopupWindow.open();
										}
									}, 500);
									break;
								} */
								
								/*********************** 新弹窗代码 *********************/
								
								// 判断后台自定义控件是否是弹窗控件
								if (this.diyData.value[i].controller == 'PopWindow') {
									
									// 赋值
									this.popWindowBox = this.diyData.value[i];
									
									// 判断图片地址是否为空
									if (this.popWindowBox.image_url.length > 0) {
										this.popWindowBox.image_url = this.$util.img(this.popWindowBox.image_url);
										
										// 判断弹出类型字段是否存在，因为是新增功能，正式数据可能没有该字段
										if (typeof _this.popWindowBox.pop_type === 'undefined') {
											_this.popWindowBox.pop_type = 1;
										}
										
										
										// 判断间隔时间字段是否存在，因为是新增功能，正式数据可能没有该字段
										if (typeof _this.popWindowBox.interval_time === 'undefined') {
											_this.popWindowBox.interval_time = 0;
										}
										
										// 默认不弹出
										_this.isPopWindowBox = false;
										
										// 针对首次弹出处理
										if (_this.popWindowBox.pop_type != 2) {
											uni.removeStorageSync('index_wap_floating_layer');
										}
										
										// 针对间隔弹出处理
										if (_this.popWindowBox.pop_type != 4) {
											uni.removeStorageSync('last_index_popwindow_time');
										}
										
										
										if (_this.popWindowBox.pop_type == 1) {
											// 不弹出, 不做处理
											
										} else if (_this.popWindowBox.pop_type == 2) {
											// 首次弹出
											
											let floating = uni.getStorageSync('index_wap_floating_layer');
											if (floating != null && floating != '') {
												floating = JSON.parse(floating);
												if (floating.closeNum < 1) {
													_this.isPopWindowBox = true;
												}
											} else {
												_this.isPopWindowBox = true;
											}
											
										} else if (_this.popWindowBox.pop_type == 3) {
											// 每次弹出
											
											_this.isPopWindowBox = true;
											
										} else if (_this.popWindowBox.pop_type == 4) {
											// 间隔弹出
											
											// 如果间隔时间小于等于0，则不弹出
											if (_this.popWindowBox.interval_time > 0) {
												
												// 上一次的弹出时间
												let last_index_popwindow_time = uni.getStorageSync('last_index_popwindow_time');
												if (last_index_popwindow_time) {
													
													last_index_popwindow_time = parseInt(last_index_popwindow_time);
													
													// （上次弹出时间 + 间隔时间）是否小于等于 当前时间
															let time = last_index_popwindow_time + (_this.popWindowBox.interval_time * 60);
															if (time <= parseInt((new Date() / 1000))) {
															 _this.isPopWindowBox = true;
														 }
												} else {
													// 首次设置间隔弹出，所以上一次的弹出时间不存在，这里要弹出
													_this.isPopWindowBox = true;
												}
											}
										}
									}
									
									// 延时执行，不然会报错
									setTimeout(() => {
										if (_this.isPopWindowBox) {
											_this.$refs.uniPopupWindow.open();
										}
									}, 500)
									
								}
								
								
							}
							uni.setNavigationBarTitle({
								title: this.diyData.global.title
							})
						}
						uni.stopPullDownRefresh();
						this.$nextTick(() => {
							if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
						});
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			closeNum() {
				this.$refs.uniPopupWindow.close();
				var closeNum = 1;
				if (uni.getStorageSync('index_wap_floating_layer')) {
					var wap_floating_cookie = JSON.parse(uni.getStorageSync('index_wap_floating_layer'));
					closeNum = ++wap_floating_cookie.closeNum;
				}
				uni.setStorageSync(
					'index_wap_floating_layer',
					JSON.stringify({
						closeNum
					})
				);
				// 记录时间
				uni.setStorageSync('last_index_popwindow_time', parseInt((new Date() / 1000)));
			},
			redirectTo(link) {
				this.$util.diyRedirectTo(link);
			},
			getWebSitefo() {
				this.webSiteInfo = uni.getStorageSync('web_site_info');
				if (this.webSiteInfo) this.webSiteInfo = JSON.parse(this.webSiteInfo);
				this.$api.sendRequest({
					url: '/api/website/info',
					success: res => {
						let data = res.data;
						if (data) {
							this.webSiteInfo = data;
							if (this.webSiteInfo.wap_status) return;
							this.$refs.uniPopupClose.open();
							uni.setStorageSync('web_site_info', JSON.stringify(this.webSiteInfo));
						}
					}
				});
			},
			// 获取默认图
			getDefaultImg() {
				this.$api.sendRequest({
					url: '/api/config/defaultimg',
					success: res => {
						let data = res.data;
						if (res.code == 0 && data) {
							uni.setStorageSync('default_img', JSON.stringify(data));
						}
					}
				});
			},
			getMemberId() {
				this.$api.sendRequest({
					url: '/api/member/id',
					success: res => {
						if (res.code >= 0) {
							this.memberId = res.data;
						}
					}
				});
			},
			onShareAppMessage(res) {
				var title = '';
				if (this.webSiteInfo) title = this.webSiteInfo.title;
				var path = '/pages/index/index/index';
				if (this.memberId) path += '?source_member=' + this.memberId;
				return {
					title: title,
					path: path,
					success: res => {},
					fail: res => {}
				};
			},
			//分发各种操作
			actionController(data){
				//获取真实路径
				let url = '/pages/index/index/index?';
				let arr = [];
				for(let i in data){
					arr.push(i + '=' + data[i]);
				}
				url += arr.join('&');
				this.trueUrl = url;
				switch(data.action){
					case 'payment_from_shop':
						this.actionPaymentFromShop(data);
						break;
				}
			},
			//代客下单
			actionPaymentFromShop(data){
				if (!uni.getStorageSync('token')) {
					this.$util.redirectTo('/pages/login/login/login', {
						back: this.trueUrl,
						redirect:'reLaunch',//返回页面后要触发onload
					});
					return;
				} 
				uni.setStorage({
					key: 'orderCreateData',
					data: {
						cart_ids: data.cart_ids,
						//区分用户自己的购物车数据
						cart_ids_source: 'shop',
					},
					success: () => {
						this.$util.redirectTo('/pages/order/payment/payment');
					}
				});
			}
		},
		onReady() {
			this.$refs.loadingCover.hide();
			// 微信公众号分享
			// #ifdef H5
			if (this.$util.isWeiXin()) {
				this.$api.sendRequest({
					url: '/wechat/api/wechat/share',
					data: {
						url: window.location.href
					},
					success: res => {
						if (res.code == 0) {
							var wxJS = new Weixin();
							wxJS.init(res.data.jssdk_config);

							let shareConfig = res.data.share_config,
								url = this.$config.h5Domain + '/pages/index/index/index';
							if (this.memberId) url += '?source_member=' + this.memberId;

							wxJS.setShareData({
								title: shareConfig.shop_param_1 + shareConfig.site_name,
								desc: shareConfig.shop_param_2 + '\r\n' + shareConfig.shop_param_3 + '\r\n' + '收藏热度：★★★★★',
								link: url,
								imgUrl: this.$util.img(shareConfig.site_logo)
							});
						}
					}
				});
			}
			// #endif
		}
	};
</script>
<style>


</style>
